package com.teradata.dashboard.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import android.content.Context;
import android.os.Environment;

/**
 * 文件操作辅助类
 * @author Administrator
 *
 */
public class FileHelper {
	
	
	public static List<String> readFile(Context context,String FileName,boolean isSD){
		List<String> rtnArr=new ArrayList<String>();
		String filePath=FileName;
		if(isSD&&Environment.getExternalStorageDirectory().equals(Environment.MEDIA_MOUNTED)){
			try {
				filePath=Environment.getExternalStorageDirectory().getCanonicalPath()+FileName;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try{
			FileInputStream fis=context.openFileInput(filePath);
			BufferedReader br=new BufferedReader(new InputStreamReader(fis));
			String line="";
			while ((line =br.readLine())!=null){
				rtnArr.add(line);
			}
			br.close();
		}catch (Exception e){
			e.printStackTrace();
		}
		return rtnArr;
	}
	
	public static int writeFile(Context context,String FileName,String content,boolean isSD){
		int rtnCnt=0;
		String filePath=FileName;
		if(isSD&&Environment.getExternalStorageDirectory().equals(Environment.MEDIA_MOUNTED)){
			try {
				filePath=Environment.getExternalStorageDirectory().getCanonicalPath()+FileName;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try{
			FileOutputStream fos=context.openFileOutput(filePath,Context.MODE_APPEND);
			BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fos));
			bw.write(content);
			bw.newLine();
			bw.close();
		}catch (Exception e){
			e.printStackTrace();
		}
		return rtnCnt;
	}
	
	public static int dropFile(Context context,String FileName,boolean isSD){
		int rtnCnt=0;
		String filePath=FileName;
		if(isSD&&Environment.getExternalStorageDirectory().equals(Environment.MEDIA_MOUNTED)){
			try {
				filePath=Environment.getExternalStorageDirectory().getCanonicalPath()+FileName;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try{
			File file=new File(filePath);
			if(file.canExecute()){
				file.delete();
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return rtnCnt;
	}
	
	
	public static Properties readProperties(Context c,String FileName) {
		 Properties props = new Properties();
		 InputStream in=null;
		 try {
			 in = c.getAssets().open(FileName);
			 props.load(in);
			} catch (IOException e) {			
				e.printStackTrace();
			}finally{
			    try {
					in.close();
				} catch (IOException e) {					
					e.printStackTrace();
			  }
			}
		 return props;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
	}

}
